package com.mall.service.commodity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.commons.common.CommonPage;
import com.mall.model.domain.PmsBrand;
import com.mall.service.commodity.mapper.PmsBrandMapper;
import com.mall.service.commodity.service.PmsBrandService;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class PmsBrandServiceImpl extends ServiceImpl<PmsBrandMapper, PmsBrand> implements PmsBrandService {
    @Autowired
    private PmsBrandMapper pmsBrandMapper;

    @Override
    public void create(PmsBrand pmsBrand) {

        pmsBrandMapper.insert(pmsBrand);
    }

    @Override
    public void deleteByIds(List<Long> ids) {

        pmsBrandMapper.deleteBatchIds(ids);
    }

    @Override
    public void deleteById(Long id) {

        pmsBrandMapper.deleteById(id);

    }

    @Override
    public CommonPage seleteList(String keyword, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<PmsBrand> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(null != keyword, PmsBrand::getName, keyword);
        queryWrapper.orderByAsc(PmsBrand::getSort).orderByAsc(PmsBrand::getFirstLetter);
        Page<PmsBrand> page = new Page<>(pageNum, pageSize);
        pmsBrandMapper.selectPage(page, queryWrapper);
        CommonPage commonPage = CommonPage.getPage(page);
        return commonPage;
    }

    @Override
    public List<PmsBrand> seleteAll() {
        LambdaQueryWrapper<PmsBrand> queryWrapper = new LambdaQueryWrapper<>();
        List<PmsBrand> list = pmsBrandMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public void updateFactory(Integer factoryStatus, List<Long> ids) {
        LambdaQueryWrapper<PmsBrand> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(PmsBrand::getId,ids);
        List<PmsBrand> list = pmsBrandMapper.selectList(queryWrapper);
        List<PmsBrand> pmsBrandList=list.stream().map((item)->{
            item.setFactoryStatus(factoryStatus);
            return item;
        }).collect(Collectors.toList());
        this.updateBatchById(pmsBrandList);
    }

    @Override
    public void updateShowStatus(Integer showStatus, List<Long> ids) {
        LambdaQueryWrapper<PmsBrand> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(PmsBrand::getId,ids);
        List<PmsBrand> list = pmsBrandMapper.selectList(queryWrapper);
        List<PmsBrand> pmsBrandList=list.stream().map((item)->{
            item.setFactoryStatus(showStatus);
            return item;
        }).collect(Collectors.toList());
        this.updateBatchById(pmsBrandList);
    }

    @Override
    public void updatePmsBrand(Long id, PmsBrand pmsBrand) {
        LambdaQueryWrapper<PmsBrand> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PmsBrand::getId,id);
        pmsBrandMapper.update(pmsBrand,queryWrapper);
    }

    @Override
    public PmsBrand findById(Long id) {
        PmsBrand pmsBrand = pmsBrandMapper.selectById(id);
        return pmsBrand;
    }


}
